import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    count: 0, // 这里存放全局的状态
    username: localStorage.getItem('username') || 'Admin', // 获取初始值，默认 'Admin'
  },
  mutations: {
    increment(state) {
      state.count++;
    },
    decrement(state) {
      state.count--;
    },
    setUsername(state, username) {
      state.username = username;
      localStorage.setItem('username', username); // 同时更新 localStorage
    },
    clearUsername(state) {
      state.username = 'Admin';
      localStorage.removeItem('username'); // 清除 localStorage 中的用户名
    },
  },
  actions: {
    incrementAsync({ commit }) {
      setTimeout(() => {
        commit("increment");
      }, 1000);
    },
  },
  modules: {
    // 你可以在这里添加更多 Vuex 模块
  },
});
